package com.huifer.wgx.service.impl;

import com.google.common.collect.Lists;
import com.huifer.wgx.model.FunctionEntity;
import com.huifer.wgx.repository.FunctionRepository;
import com.huifer.wgx.service.FunctionService;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.boot.spi.InFlightMetadataCollector.EntityTableXref;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * <p>Title : FunctionServiceImpl </p>
 * <p>Description : </p>
 *
 * @author huifer
 * @date 2019-03-18
 */
@Service
public class FunctionServiceImpl implements FunctionService {

    @Autowired
    private FunctionRepository functionRepository;


    /**
     * 创建一个职能
     *
     * @param function 职能
     * @return 职能 {@link FunctionEntity}
     */
    @Override
    public FunctionEntity saveFunction(FunctionEntity function) {
        try {

            FunctionEntity doSave = new FunctionEntity();

            FunctionEntity byFunctionName = functionRepository
                    .findByFunctionName(function.getFunctionName());

            if (byFunctionName == null) {
                doSave.setFunctionName(function.getFunctionName());

                return functionRepository.save(doSave);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /**
     * 查找所有职能
     *
     * @return 职能列表
     */
    @Override
    public List<FunctionEntity> findAll() {
        List<FunctionEntity> functionList = Lists.newArrayList(functionRepository.findAll());
        return functionList;
    }

    /**
     * 分页查询
     *
     * @param pageNum 页面号
     * @param pageSize 页面大小
     * @return 职能列表
     */
    @Override
    public List<FunctionEntity> list(Integer pageNum, Integer pageSize) throws Exception {

        PageRequest of = PageRequest.of(pageNum - 1, pageSize);
        Page<FunctionEntity> all = functionRepository.findAll(of);
        List<FunctionEntity> a = new ArrayList<>();
        all.forEach(s -> {
            a.add(s);
        });

        return a;
    }

    /**
     * 更新职能,根据新老职能名称
     *
     * @param oldFunction 旧职能名称
     * @param newFunction 新职能名称
     * @return 职能
     */
    @Override
    public FunctionEntity updateFunction(String oldFunction, String newFunction) {
        try {
            FunctionEntity byFunctionName = functionRepository.findByFunctionName(oldFunction);
            if (byFunctionName != null) {

                byFunctionName.setFunctionName(newFunction);
                FunctionEntity save = functionRepository.save(byFunctionName);
                return save;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * . 更新职能名称,更具职能id
     *
     * @param funcId 职能id
     * @param newFunction 新职能名称
     * @return 职能
     */
    @Override
    public FunctionEntity updateFunction(Long funcId, String newFunction) {
        try {

            FunctionEntity functionEntity = functionRepository.findById(funcId).get();
            if (functionEntity != null) {

                functionEntity.setFunctionName(newFunction);
                FunctionEntity save = functionRepository.save(functionEntity);
                return save;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据id删除职能
     *
     * @param funcId 职能id
     * @return 职能
     */
    @Override
    public FunctionEntity deleteFunction(Long funcId) {
        try {
            FunctionEntity functionEntity = functionRepository.findById(funcId).get();
            if (functionEntity != null) {
                functionRepository.delete(functionEntity);
                return functionEntity;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据职能名称删除职能
     *
     * @param funcName 职能名称
     * @return 职能
     */
    @Override
    public FunctionEntity deleteFunction(String funcName) {
        try {

            FunctionEntity byFunctionName = functionRepository.findByFunctionName(funcName);
            if (byFunctionName != null) {
                functionRepository.deleteById(byFunctionName.getFunctionId());
                return byFunctionName;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
